package org.beetl.sql.gen.simple.controller;

import org.beetl.core.Template;
import org.beetl.sql.clazz.kit.StringKit;
import org.beetl.sql.gen.BaseProject;
import org.beetl.sql.gen.Entity;
import org.beetl.sql.gen.SourceConfig;
import org.beetl.sql.gen.simple.BaseTemplateSourceBuilder;

import java.io.Writer;
import java.util.Arrays;

/**
 * 表对应实体的controller代码，参考controller.btl
 */
public class ControllerSourceBuilder extends BaseTemplateSourceBuilder {

	/**
	 * 指定模板的路径
	 */
	public static  String mapperPath = "beetlsqlTemplates/controller/controller.btl";


	public ControllerSourceBuilder(String tableName) {
		super("controller."+tableName);
	}

	//Override
	@Override
	public void generate(BaseProject project, SourceConfig config, Entity entity) {


		Template template = groupTemplate.getTemplate(mapperPath);
		String tableName = entity.getName();
		String tfTableName = StringKit.toLowerCaseFirstOne(entity.getName());
		template.binding("tableName", tableName);
		template.binding("conName", tfTableName + "Con");
		template.binding("conClassName", tableName + "Controller");
		template.binding("serClassName", tableName + "Service");
		template.binding("serName", tfTableName + "Service");
		template.binding("package", project.getBasePackage(this.name));
		template.binding("beanName", entity.getName()+"Bean");
		template.binding("servicePackage", project.getBasePackage("service.")+entity.getTableName()+".*");
		//得到生成的entity的包
		String entityPkg = project.getBasePackage("bean");
		String mapperHead =  entityPkg + ".*" ;
		template.binding("imports", Arrays.asList(mapperHead));
		Writer writer = project.getWriterByName(this.name,entity.getName()+"Controller.java");
		template.renderTo(writer);



	}
}
